/*
 * Layer.cpp
 *
 *  Created on: Aug 26, 2011
 *      Author: mengwei.chua
 */

#include "Layer.h"
#include "ParserHelper.h"

namespace projectcos
{
namespace tile
{
namespace parser
{

Layer::Layer()
: m_Name("")
, m_Opacity(0.0f)
, m_Visible(false)
{
	m_DataList.clear();
}

Layer::~Layer()
{
	m_DataList.clear();
}

bool Layer::Parse(const TiXmlElement* element)
{
	if(NULL != element)
	{
		// Parse attribute
		TIXML_QUERY_STRING(element, "name", m_Name);
		TIXML_QUERY_FLOAT(element, "opacity", m_Opacity);
		TIXML_QUERY_BOOL(element, "visible", m_Visible);

		// Parse child elements
		const TiXmlElement* child = element->FirstChildElement("properties");
		if(NULL != child)
		{
			m_Properties.Parse(child);
		}

		child = element->FirstChildElement("data");
		while(NULL != child)
		{
			Data data;
			if(data.Parse(child))
			{
				m_DataList.push_back(data);
			}

			child = child->NextSiblingElement("data");
		}
	}

	return true;
}

std::string Layer::GetName() const
{
	return m_Name;
}

float Layer::GetOpacity() const
{
	return m_Opacity;
}

bool Layer::GetVisible() const
{
	return m_Visible;
}

void Layer::GetProperties(Properties& properties) const
{
	properties = m_Properties;
}

void Layer::GetData(std::deque<Data>& data) const
{
	data = m_DataList;
}


Layer::Data::Data()
: m_Encoding("")
, m_Compression("")
, m_Data("")
{
}

Layer::Data::~Data()
{

}

bool Layer::Data::Parse(const TiXmlElement* element)
{
	if(NULL != element)
	{
		m_Encoding = element->Attribute("encoding");
		m_Compression = element->Attribute("compression");

		m_Data = element->GetText();
	}
	else
	{
		return false;
	}

	return true;
}

std::string Layer::Data::GetEncoding() const
{
	return m_Encoding;
}

std::string Layer::Data::GetCompression() const
{
	return m_Compression;
}

std::string Layer::Data::GetData() const
{
	return m_Data;
}

} // parser
} // tile
} // projectcos
